package com.mzj.saas.mservice.qiyuesuo.service;

import com.mzj.saas.mservice.qiyuesuo.vo.CompanyAuthVO;
import com.mzj.saas.mservice.qiyuesuo.vo.PersonalAuthVO;
import com.qiyuesuo.sdk.v2.SdkClient;
import com.qiyuesuo.sdk.v2.bean.User;
import com.qiyuesuo.sdk.v2.http.FileItem;
import com.qiyuesuo.sdk.v2.http.StreamFile;
import com.qiyuesuo.sdk.v2.json.JSONUtils;
import com.qiyuesuo.sdk.v2.request.*;
import com.qiyuesuo.sdk.v2.response.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 契约锁接口-认证
 * 
 * @author hp
 *
 */
@Service
public class QiYueSuoAuthService {

	private Logger logger = LoggerFactory.getLogger(QiYueSuoAuthService.class);

	@Autowired
	private SdkClient sdkClient;

	/**
	 * 获取个人认证链接 调用此接口来获取个人认证页面链接，链接只能打开一次，链接有效期5分钟，页面会话有效期15分钟
	 * https://open.qiyuesuo.com/document?id=2657161738167787938
	 * 
	 * return UserAuthPageResult code = 0:请求成功 authUrl:认证链接 code != 0:请求失败
	 * code:错误码 message:错误信息
	 */
	public UserAuthPageResult personalauth(PersonalAuthVO vo) {
		if (StringUtils.isBlank(vo.getContact()) || StringUtils.isBlank(vo.getContactType())) {
			return null;
		}
		UserAuthPageRequest request = new UserAuthPageRequest();

		if (StringUtils.isNotBlank(vo.getMode())) {
			request.setMode(vo.getMode());
		}
		if (StringUtils.isNotBlank(vo.getCallbackUrl())) {
			request.setCallbackUrl(vo.getCallbackUrl());
		}
		request.setUser(new User(vo.getContact(), vo.getContactType()));
		String response = sdkClient.service(request);
		SdkResponse<UserAuthPageResult> responseObj = JSONUtils.toQysResponse(response, UserAuthPageResult.class);

		return responseObj.getResult();

		// if(responseObj.getCode() == 0) {
		// logger.info("请求成功，认证链接:{}", responseObj.getResult().getAuthUrl());
		// } else {
		// logger.info("请求失败，错误码:{}，错误信息:{}", responseObj.getCode(),
		// responseObj.getMessage());
		// }
	}

	/**
	 * 查询个人实名认证的结果 https://open.qiyuesuo.com/document?id=2657161865565581536
	 * 
	 * * return UserAuthPageResult code = 0:请求成功 realName:是否认证成功 code != 0:请求失败
	 * code:错误码 message:错误信息
	 */
	public UserAuthResult queryPersonalauth(PersonalAuthVO vo) {
		if (StringUtils.isBlank(vo.getContact()) || StringUtils.isBlank(vo.getContactType())) {
			return null;
		}
		UserAuthResultRequest request = new UserAuthResultRequest(new User(vo.getContact(), vo.getContactType()));
		String response = sdkClient.service(request);
		SdkResponse<UserAuthResult> responseObj = JSONUtils.toQysResponse(response, UserAuthResult.class);
		return responseObj.getResult();
	}

	/**
	 * 获取企业认证链接(带营业执照) 请求地址（H5）
	 * https://open.qiyuesuo.com/document?id=2657162199159546445
	 */
	public CompanyAuthPageResult h5pagewithlicense(CompanyAuthVO vo) {
		if (StringUtils.isBlank(vo.getCompanyName()) || StringUtils.isBlank(vo.getContact())
				|| StringUtils.isBlank(vo.getContactType()) || vo.getFile() == null) {
			return null;
		}
		try {
			User applicant = new User(vo.getName(), vo.getContact(), vo.getContactType());
			String applicantInfo = JSONUtils.toJson(applicant);
			CompanyAuthLicenseH5PageRequest request = new CompanyAuthLicenseH5PageRequest();
			request.setCompanyName(vo.getCompanyName());
			request.setApplicantInfo(applicantInfo);
			if (StringUtils.isNotBlank(vo.getCallbackUrl())) {
				request.setCallbackUrl(vo.getCallbackUrl());
			}
			FileItem fileItem = new StreamFile(vo.getFile().getInputStream());
			request.setLicense(fileItem);
			String response = sdkClient.service(request);
			SdkResponse<CompanyAuthPageResult> pageResultResponse = JSONUtils.toQysResponse(response,
					CompanyAuthPageResult.class);
			return pageResultResponse.getResult();
		} catch (Exception e) {
			logger.error("获取企业认证链接(带营业执照)异常" + e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 获取企业认证链接(带营业执照) 请求地址（PC）
	 * https://open.qiyuesuo.com/document?id=2657162199159546445
	 */
	public CompanyAuthPageResult pcpagewithlicense(CompanyAuthVO vo) {
		if (StringUtils.isBlank(vo.getCompanyName()) || StringUtils.isBlank(vo.getContact())
				|| StringUtils.isBlank(vo.getContactType()) || vo.getFile() == null) {
			return null;
		}
		try {
			User applicant = new User(vo.getName(), vo.getContact(), vo.getContactType());
			String applicantInfo = JSONUtils.toJson(applicant);
			CompanyAuthLicensePCPageRequest request = new CompanyAuthLicensePCPageRequest();
			request.setCompanyName(vo.getCompanyName());
			request.setApplicantInfo(applicantInfo);
			if (StringUtils.isNotBlank(vo.getCallbackUrl())) {
				request.setCallbackUrl(vo.getCallbackUrl());
			}
			FileItem fileItem = new StreamFile(vo.getFile().getInputStream());
			request.setLicense(fileItem);
			String response = sdkClient.service(request);
			SdkResponse<CompanyAuthPageResult> pageResultResponse = JSONUtils.toQysResponse(response,
					CompanyAuthPageResult.class);
			return pageResultResponse.getResult();
		} catch (Exception e) {
			logger.error("获取企业认证链接(带营业执照)异常" + e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 获取企业认证链接(无需营业执照) 请求地址（H5）
	 * 
	 * @return
	 */
	public CompanyAuthPageResult companyauthh5page(CompanyAuthVO vo) {
		if (StringUtils.isBlank(vo.getCompanyName()) || StringUtils.isBlank(vo.getContact())
				|| StringUtils.isBlank(vo.getContactType())) {
			return null;
		}

		User applicant = new User(vo.getName(), vo.getContact(), vo.getContactType());
		CompanyAuthH5PageRequest request = new CompanyAuthH5PageRequest(vo.getCompanyName(), applicant);
		if (StringUtils.isNotBlank(vo.getCallbackUrl())) {
			request.setCallbackUrl(vo.getCallbackUrl());
		}
		String response = sdkClient.service(request);
		SdkResponse<CompanyAuthPageResult> pageResultResponse = JSONUtils.toQysResponse(response,
				CompanyAuthPageResult.class);
		return pageResultResponse.getResult();
	}

	/**
	 * 获取企业认证链接(无需营业执照) 请求地址（PC）
	 * 
	 * @return
	 */
	public CompanyAuthPageResult companyauthpcpage(CompanyAuthVO vo) {
		if (StringUtils.isBlank(vo.getCompanyName()) || StringUtils.isBlank(vo.getContact())
				|| StringUtils.isBlank(vo.getContactType())) {
			return null;
		}

		User applicant = new User(vo.getName(), vo.getContact(), vo.getContactType());
		CompanyAuthPCPageRequest request = new CompanyAuthPCPageRequest(vo.getCompanyName(), applicant);
		if (StringUtils.isNotBlank(vo.getCallbackUrl())) {
			request.setCallbackUrl(vo.getCallbackUrl());
		}
		String response = sdkClient.service(request);
		SdkResponse<CompanyAuthPageResult> pageResultResponse = JSONUtils.toQysResponse(response,
				CompanyAuthPageResult.class);
		return pageResultResponse.getResult();
	}

	/**
	 * 查询企业实名认证的结果 https://open.qiyuesuo.cn/document?id=2657095461172678968
	 * 待查询的认证公司名称 或 认证请求ID
	 */
	public CompanyAuthResult queryCompanyauth(String companyName, String requestId) {

		if (StringUtils.isBlank(companyName) && StringUtils.isBlank(requestId)) {
			return null;
		}

		CompanyAuthResultRequest request = new CompanyAuthResultRequest();

		if (StringUtils.isNotBlank(companyName)) {
			request.setCompanyName(companyName);
		} else if (StringUtils.isNotBlank(requestId)) {
			request.setRequestId(requestId);
		}
		String response = sdkClient.service(request);
		SdkResponse<CompanyAuthResult> resultResponse = JSONUtils.toQysResponse(response, CompanyAuthResult.class);
		return resultResponse.getResult();
	}
}